package com.appspot.bs24event.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.appspot.bs24event.common.Constants;
import com.appspot.bs24event.common.Constants.USER_AGENT;
import com.appspot.bs24event.util.CommonUtil;

/**
 * UserAgentFilter.
 *
 * @version $Id: InitFilter.java 66 2011-05-13 11:07:06Z bs24event@gmail.com $
 *
 */
public class UserAgentFilter implements Filter {

    /** ログ */
    protected static final Logger logger = Logger.getLogger(UserAgentFilter.class.getName());

    @Override
    public void init(FilterConfig config) throws ServletException {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,  FilterChain chain)
    throws IOException, ServletException
    {
        HttpServletRequest  request  = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpSession session = request.getSession(true);

        // UserAgentの取得
        String userAgent = CommonUtil.replaceNull(request.getHeader("User-Agent"));

        // UserAgentから端末の判定
        if ( userAgent.indexOf("Android")!=-1 && userAgent.indexOf("Mobile")!=-1 ) {
            // Androidの携帯端末のみ。タブレットはPC扱いにする。
            if (logger.isDebugEnabled()) logger.debug("Android. userAgent = "+userAgent);
            session.setAttribute(Constants.USER_AGENT, USER_AGENT.ANDROID);
        } else if ( userAgent.indexOf("iPhone")!=-1 ) {
            if (logger.isDebugEnabled()) logger.debug("iPhone. userAgent = "+userAgent);
            session.setAttribute(Constants.USER_AGENT, USER_AGENT.IPHONE);
        } else if ( userAgent.indexOf("DoCoMo")!=-1 ) {
            if (logger.isDebugEnabled()) logger.debug("docomo. userAgent = "+userAgent);
            session.setAttribute(Constants.USER_AGENT, USER_AGENT.DOCOMO);
        } else if ( userAgent.indexOf("J-PHONE")!=-1 || userAgent.indexOf("MOT-C")!=-1 || userAgent.indexOf("MOT-V")!=-1 || userAgent.indexOf("Vodafone")!=-1 || userAgent.indexOf("SoftBank")!=-1 ) {
            if (logger.isDebugEnabled()) logger.debug("SoftBank. userAgent = "+userAgent);
            session.setAttribute(Constants.USER_AGENT, USER_AGENT.SOFTBANK);
        } else if ( userAgent.indexOf("UP.Browser")!=-1 || userAgent.indexOf("KDDI-")!=-1 ) {
            if (logger.isDebugEnabled()) logger.debug("au. userAgent = "+userAgent);
            session.setAttribute(Constants.USER_AGENT, USER_AGENT.AU);
        } else {
            if (logger.isDebugEnabled()) logger.debug("PC. userAgent = "+userAgent);
            session.setAttribute(Constants.USER_AGENT, USER_AGENT.PC);
        }

        chain.doFilter(request, response);
    }
}
